<?php

/*
    Genres controller controls all movie genres.
    Users can look for movies which belong to a certain genre.
    Admins can add, remove or edit genres.
 */

class Controller_Genre extends Controller_Template {
    private $user_data;
    private $user_id;
    private $language;
    
    public function before() {
        $this->language = Cookie::get((Auth::check() ? 'user' : 'guest') . '_language', 'lv');
        Config::set("language", $this->language);
        Lang::load('main', null, $this->language, true);
        
        parent::before();
        $this->user_data = Auth::instance();
        $this->user_id = $this->user_data->get_user_id();
        
        $this->template->language = $this->language;
        $this->template->genres = Model_Orm_Genre::find('all');
        $this->template->cinema_info = Model_Orm_Cinema::find('first');
    }
    
    //Specifies the default controller page.
    public function action_index() {
        return Response::redirect('/genre/manage');
    }
    
    public function action_manage() {   
        if(!Auth::member(100)) {
            Response::redirect('/');
        }
        
        $genres = Model_Orm_Genre::find('all');
        
        $view_genre = View::forge('genre/manage');
        $view_genre->set("genres", $genres);
        $this->template->page_content = $view_genre;
    }
    
    public function action_delete($genre_id = null) {
        if(!Auth::member(100)) {
            Response::redirect('/');
        }
        
        is_null($genre_id) and Response::redirect('/');
        
        $genre_to_delete = Model_Orm_Genre::find($genre_id);
        
        is_null($genre_to_delete) and Response::redirect('/');
        
        $movie_genres = DB::select()->from('movie_genres')
                ->where('genre_id', '=', $genre_id)
                ->execute();
        
        foreach($movie_genres as $genre) {
            $genre_obj = Model_Orm_Moviegenre::find($genre['id']);
            $genre_obj->delete();
        }
        
        //Deletes from the database.
        $genre_to_delete->delete();
        
        Response::redirect('genre/manage');
    }
    
    public function action_new() {
        if(!Auth::member(100)) {
            Response::redirect('/');
        }
        
        if(Input::method() == 'POST') {
            $validation = Model_Orm_Genre::validate('add');
            
            if($validation->run()) {
                $new_genre = new Model_Orm_Genre();
                $new_genre->genre_lv = $validation->validated("Genre_Lv");
                $new_genre->genre_en = $validation->validated("Genre_En");
                
                $new_genre->save();
            }
        }
        
        Response::redirect('genre/manage');
    }
    
    public function action_edit() {
        if(!Auth::member(100)) {
            Response::redirect('/');
        }
        
        if(Input::method() == 'POST') {
            $genre_id = Input::post('Genre_ID');
            
            is_null($genre_id) and Response::redirect('genre/manage');
            
            $genre_info = Model_Orm_Genre::find($genre_id);
            
            is_null($genre_info) and Response::redirect('genre/manage');
            
            $validation = Model_Orm_Genre::validate('edit');
            
            if($validation->run()) {
                $genre_info->genre_lv = $validation->validated("Genre_Lv");
                $genre_info->genre_en = $validation->validated("Genre_En");
                
                $genre_info->save();
            } else {
                Session::set_flash('error', 'Ievaddatu kļūda !');
                Response::redirect('genre/manage');
            }
        }
        
        Response::redirect('genre/manage');
    }
    
    public function action_view($genre_id = null) {
        is_null($genre_id) and Response::redirect('/');
        
        $genre_info = Model_Orm_Genre::find($genre_id);
        
        is_null($genre_info) and Response::redirect('/');
        
        //Finds all genres which match the WHERE clasue.
        $movie_genres = Model_Orm_Moviegenre::find('all', array('where' => array(array('genre_id', $genre_id))));
        
        $view_genre_view = View::forge('genre/view');
        $view_genre_view->set("genre_info", $genre_info);
        $view_genre_view->set("movie_genres", $movie_genres);
        $view_genre_view->set("language", $this->language);
        $this->template->page_content = $view_genre_view;   
    }
}
